/*
 * context.h
 *
 *  Created on: 2024年6月6日
 *      Author: liangjp
 */

#ifndef JNI_CAN_CONTEXT_H_
#define JNI_CAN_CONTEXT_H_

#include <linux/can.h>
#include <stdint.h>
#include <vector>
#include <string>
#include <functional>

// 定义 ByteBits 联合体
typedef union {
    uint8_t byte;  // 整个字节
    struct {
        unsigned bit0 :1;  // 第0位
        unsigned bit1 :1;  // 第1位
        unsigned bit2 :1;  // 第2位
        unsigned bit3 :1;  // 第3位
        unsigned bit4 :1;  // 第4位
        unsigned bit5 :1;  // 第5位
        unsigned bit6 :1;  // 第6位
        unsigned bit7 :1;  // 第7位
    } bits;  // 位字段
} ByteBits;

// 重载 == 运算符
inline bool operator==(const ByteBits& lhs, const ByteBits& rhs) {
    return lhs.byte == rhs.byte;
}

// 重载 != 运算符
inline bool operator!=(const ByteBits& lhs, const ByteBits& rhs) {
    return !(lhs == rhs);
}

typedef struct {
	ByteBits ACC;
	uint16_t CumulativeFuel;				//累计平均油耗
	uint8_t gearRank;			//1-8档
	uint8_t gear;				//挡位
	uint8_t gearGrade;			//挡位等级0-8
	uint8_t compass;		//指南针
	uint8_t Hanging;		//悬挂信息
} V_Condition;				//ACC挡位信息

typedef struct {
	uint16_t rotation_; 		//发动机转速
	uint16_t speed_; 	//瞬时车速
	uint16_t Instant_fuel_consumption;			//瞬时油耗
	bool tyreDirection;		//方向盘转动的方向，true:左转,false:右转
	uint8_t tyreAngle;		//方向盘转角,区分正负
} Vehicle_info1;

typedef struct {
	uint16_t RangeEndurance; //续航里程

	uint8_t Display_Mode; 			//油箱类型
	uint8_t Oil_percentage;			//剩余油量百分比

	int CoolantTemperature;	//冷却液温度
	int TransmissionTemp;	//变速箱温度
} Vehicle_info2;


typedef struct {
	int OutdoorTemp; 		//室外温度
	int EngineOilTemp; 		//机油温度
	uint16_t OilPressure;		//机油压力
	uint16_t BatteryVoltage;		//电池电压
} Vehicle_info3;	//车身信息3

typedef struct {
	int odo;	//总里程
	uint8_t oilLife; 			//机油寿命
	uint16_t SubtotalMileage;	//自上次机油寿命复位行驶里程
} Vehicle_ODO;	//车身信息4

typedef struct {
	int SubtotalMileageA;		//小计里程A
	int SubtotalMileageB; 		//小计里程B
} Vehicle_Subtotal;	//车身信息5

typedef struct {
	ByteBits fault0;				//故障 data0
	ByteBits fault1;		//指示灯 data1
	ByteBits fault2;				//故障
	ByteBits fault3;		//指示灯
	ByteBits fault4;				//故障  data4
	ByteBits fault5;
} Fault_Indicator;

typedef struct {
	uint16_t AverageFuel; 		//平均油耗
	int TravelTime; 		//行驶时间
	uint16_t AverageVelocity;		//平均速度
} SubtotalA;	//小计行程A信息

typedef struct {
	uint16_t AverageFuel; 		//平均油耗
	int TravelTime; 		//行驶时间
	uint16_t AverageVelocity;		//平均速度
} SubtotalB;	//小计行程B信息

typedef struct {
	uint16_t freeTime; 		//空闲时间
	uint16_t RunningTime; 		//运行时间
	uint16_t TotalTime;		//总时间
} EngineTime;	//发动机时间信息

typedef struct {
	bool vehicleType;	//车辆类型
	ByteBits TireFault;
	uint16_t RL_InnerSide;		//胎压左后内侧
	uint16_t RR_InnerSide;		//胎压右后内侧
} Tire_PressureFault;

typedef struct {
	uint16_t FL_TP;		//前左轮胎压
	uint16_t FR_TP;		//前右轮胎压
	uint16_t RL_TP;		//后左轮胎压
	uint16_t RR_TP;		//后右轮胎压
} Tire_Pressure;

typedef struct {
	uint8_t TrailerStatus;	//拖车状态
	uint8_t Gain;
	uint8_t AntiTrailer;	//反拖车
	int TrailerDistance;		//拖车行驶距离
} TrailerInformation;

typedef struct {
	uint8_t FL_Radar;		//前左雷达信号
	uint8_t FR_Radar;		//前右雷达信号
	uint8_t FL_RadarM;		//前左中雷达信号
	uint8_t FR_RadarM;		//前右中雷达信号
	uint8_t RL_RadarM;		//后左中雷达信号
	uint8_t RR_RadarM;		//后右中雷达信号
	uint8_t RL_Radar;		//后左雷达信号
	uint8_t RR_Radar;		//后右雷达信号
	uint8_t FL_Radar_blink;		//前左雷达信号
	uint8_t FR_Radar_blink;		//前右雷达信号
	uint8_t FL_RadarM_blink;		//前左中雷达信号
	uint8_t FR_RadarM_blink;		//前右中雷达信号
	uint8_t RL_RadarM_blink;		//后左中雷达信号
	uint8_t RR_RadarM_blink;		//后右中雷达信号
	uint8_t RL_Radar_blink;		//后左雷达信号
	uint8_t RR_Radar_blink;		//后右雷达信号
} RadarInformation;

typedef enum {
	KM,
	MILE
}UNIT_D;	//距离

typedef enum {
	KMH,
	MPH
}UNIT_S;	//速度

typedef enum {
	BAR,
	KPA,
	PSI
}UNIT_P;	//胎压

typedef enum {
	CELSIUS,	//℃
	DEGREE		//℉
}UNIT_T;	//温度

typedef enum {
	L_100,
	KM_L,
	MPG_UK,
	MPG_US
}UNIT_O;	//油耗单位


typedef struct {
	UNIT_D s_distance = KM;
	UNIT_S s_speed = KMH;
	UNIT_P s_pressure = BAR;
	UNIT_T s_temperature = CELSIUS;
	UNIT_O s_fuel = L_100;
} UnitStatus;


typedef struct {
	uint8_t AssistedType;	//辅助驾驶类型
	uint8_t DistanceCar;	//跟车距离
	bool isCar;
	bool twinkle;
	uint16_t AssisedSpeed;
} Assisted_Driving;


typedef struct {
	uint16_t AddOrDel;
	uint16_t FaultID;
} FaultPrompt;

typedef struct {
	uint8_t Hour;
	uint8_t Minute;
} Time_Synchronization_Info;		//时间同步信息

typedef enum{
	KEY_UP = 2,		//上按键
	KEY_DOWN,		//下
	KEY_LEFT,		//左
	KEY_RIGHT,		//右
}key_operation_e;

typedef enum{
	KEY_O,	//松开
	KEY_1,	//短按
	KEY_2	//长按
}key_status;

typedef struct {
	key_status pre_status;		//上一次按键状态
	key_status status;			//按键状态
	key_operation_e operation;	//按键操作
}key_value_t;


typedef struct {
    void (*condition_cb)(V_Condition&);
    void (*version_info_cb)(char*);
    void (*vehicle_info1_cb)(Vehicle_info1&);  // 0x1FFF0002
    void (*vehicle_info2_cb)(Vehicle_info2&);  // 0x1FFF0003
    void (*vehicle_info3_cb)(Vehicle_info3&);  // 0x1FFF0004

    void (*vehicle_odo_cb)(Vehicle_ODO&);
    void (*vehicle_subtotal_cb)(Vehicle_Subtotal&);
    void (*fault_indicator_cb)(Fault_Indicator&);
    void (*SubtotalA_cb)(SubtotalA&);
    void (*SubtotalB_cb)(SubtotalB&);

    void (*EngineTime_cb)(EngineTime&);
    void (*tire_pressure_cb)(Tire_Pressure&);
    void (*Tire_PressureFault_cb)(Tire_PressureFault&);
    void (*TrailerInformation_cb)(TrailerInformation&);
    void (*Assisted_Driving_cb)(Assisted_Driving&);
    void (*FaultPrompt_cb)(FaultPrompt&);
    void (*RadarInformation_cb)(RadarInformation&);
    void (*UnitStatus_cb)(UnitStatus&);
    void (*time_sync_info_cb)(Time_Synchronization_Info&);
    void (*key_value_cb)(key_value_t&);

    void (*media_info_cb)(char*);
} CanDataCb;

namespace can {

std::vector<uint16_t> getFaultList();
void startup();
void add_cb(CanDataCb *cb);
void remove_cb(CanDataCb *cb);

void canSend(uint32_t can_id, uint8_t dlc, uint8_t * data);

void parseProtocol(const can_frame& canFrame);

}
#endif /* JNI_CAN_CONTEXT_H_ */
